package com.zz.common.codeGenerator.controller;

import com.zz.common.base.dto.PageListResultDto;
import com.zz.common.base.dto.ResponseDTO;
import com.zz.common.base.dto.ResultDto;
import com.zz.common.codeGenerator.dto.GenTableColumnDto;
import com.zz.common.codeGenerator.dto.GenTableDto;
import com.zz.common.codeGenerator.service.GenTableService;
import com.zz.common.constants.ApiConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 代码生成业务表 服务接口类
 *
 * @author zhang.lei
 * @since 2025-08-18 20:31:13
 */
@Api(tags = "代码生成业务表服务接口类")
@RestController
@RequestMapping("${service-name.codeGen}/code-generator/v1.0/gen-table")
public class GenTableController {

    @Resource
    private GenTableService genTableService;

    @RequestMapping(value = "/query-with-page", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-分页查询", notes = "传入分页信息")
    public PageListResultDto<GenTableDto> queryWithPage(@RequestBody GenTableDto genTableDto) {
        PageListResultDto<GenTableDto> res;
        res = genTableService.queryWithPage(genTableDto, genTableDto.getQueryParam());
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/query-with-no-page", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-不分页查询", notes = "传入查询条件信息")
    public ResponseDTO<List<GenTableDto>> queryWithNoPage(@RequestBody GenTableDto genTableDto) {
        ResponseDTO<List<GenTableDto>> res = new ResponseDTO<>();
        res.setData(genTableService.queryWithNoPage(genTableDto));
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/query-all", method = RequestMethod.GET)
    @ApiOperation(value = "代码生成业务表-查询所有", notes = "")
    public ResponseDTO<List<GenTableDto>> queryAll() {
        ResponseDTO<List<GenTableDto>> res = new ResponseDTO<>();
        res.setData(genTableService.queryAll());
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "代码生成业务表-查询ById", notes = "传入id")
    public ResponseDTO<GenTableDto> queryById(@PathVariable("id") String id) {
        ResponseDTO<GenTableDto> res = new ResponseDTO<>();
        GenTableDto result = genTableService.queryById(id);
        res.setData(result);
        return res;
    }

    @RequestMapping(value = "/query-by-ids", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-查询ByIds", notes = "传入ids")
    public ResponseDTO<List<GenTableDto>> queryByIds(@RequestBody List<String> ids) {
        ResponseDTO<List<GenTableDto>> res = new ResponseDTO<>();
        res.setData(genTableService.queryByIds(ids));
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-创建", notes = "创建")
    public ResultDto save(@RequestBody GenTableDto genTableDto) {
        ResultDto res = new ResultDto();
        genTableService.save(genTableDto);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/save-batch", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-创建", notes = "创建")
    public ResultDto saveBatch(@RequestBody List<GenTableDto> genTableDtos) {
        ResultDto res = new ResultDto();
        genTableService.saveBatch(genTableDtos);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/modify/{currentLocale}", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-修改", notes = "修改")
    public ResultDto modify(@RequestBody GenTableDto genTableDto, @PathVariable("currentLocale") String currentLocale) {
        ResultDto res = new ResultDto();
        genTableService.modify(genTableDto, currentLocale);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/modify-batch", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-修改", notes = "修改")
    public ResultDto modifyBatch(@RequestBody List<GenTableDto> genTableDtos) {
        ResultDto res = new ResultDto();
        genTableService.modifyBatch(genTableDtos);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "代码生成业务表-删除ById", notes = "传入id")
    public ResultDto delete(@PathVariable("id") String id) {
        ResultDto res = new ResultDto();
        genTableService.delete(id);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/delete-batch", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-批量删除", notes = "传入id")
    public ResultDto deleteBatch(@RequestBody List<String> ids) {
        ResultDto res = new ResultDto();
        genTableService.deleteBatch(ids);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/select-db-tables", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-查询数据库表", notes = "传入数据库信息")
    public PageListResultDto<GenTableDto> selectDbTables(@RequestBody GenTableDto genTableDto) {
        PageListResultDto<GenTableDto> res = new PageListResultDto<>();
        res = genTableService.selectDbTables(genTableDto, genTableDto.getQueryParam());
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/create-table-by-sql/{currentLocale}", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-根据sql生成", notes = "传入sql")
    ResultDto createTableBySql(@RequestParam("sql") String sql, @PathVariable("currentLocale") String currentLocale) {
        ResultDto res = new ResultDto();
        genTableService.createTableBySql(sql, currentLocale);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/import-table/{currentLocale}", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-导入", notes = "")
    ResultDto importTable(@RequestBody List<String> tableNames, @PathVariable("currentLocale") String currentLocale) {
        ResultDto res = new ResultDto();
        genTableService.importTable(tableNames, currentLocale);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/preview/{tableId}", method = RequestMethod.GET)
    @ApiOperation(value = "代码生成业务表-预览代码", notes = "")
    ResponseDTO<Map<String, String>> preview(@PathVariable("tableId") String tableId) {
        ResponseDTO<Map<String, String>> res = new ResponseDTO<>();
        Map<String, String> result = genTableService.preview(tableId);
        res.setData(result);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/synch-db/{tableName}/{currentLocale}", method = RequestMethod.GET)
    @ApiOperation(value = "代码生成业务表-根据数据库同步", notes = "")
    ResultDto synchDb(@PathVariable("tableName") String tableName, @PathVariable("currentLocale") String currentLocale) {
        ResultDto res = new ResultDto();
        genTableService.synchDb(tableName, currentLocale);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/batch-gen-code", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-生成代码", notes = "")
    void batchGenCode(@RequestBody List<String> tableIds) throws IOException {
        genTableService.batchGenCode(tableIds);
    }

    @RequestMapping(value = "/batch-true-delete", method = RequestMethod.POST)
    @ApiOperation(value = "代码生成业务表-批量物理删除", notes = "")
    ResultDto batchTureDelete(@RequestBody List<String> ids) {
        ResultDto res = new ResultDto();
        genTableService.batchTureDelete(ids);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/query-columns-by-table-name/{tableName}", method = RequestMethod.GET)
    @ApiOperation(value = "代码生成业务表-根据表名查询所有列", notes = "")
    ResponseDTO<List<GenTableColumnDto>> queryColumnsByTableName(@PathVariable("tableName") String tableName) {
        ResponseDTO<List<GenTableColumnDto>> res = new ResponseDTO<>();
        res.setData(genTableService.queryColumnsByTableName(tableName));
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

}